package server;

import java.io.IOException;
import java.io.InputStream;

/**
 * 根据inputStream输入流进行封装Request对象
 */
public class Request {

    private String host;

    private String method; //比如GET POST

    private String url; // 比如 /index.html

    private InputStream inputStream;

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public Request(){

    }

    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;

        int count = 0;
        while (count == 0){
            count = inputStream.available();
        }

        byte[] bytes = new byte[count];
        inputStream.read(bytes);

        String inputStr = new String(bytes);

        String[] split = inputStr.split("\\n");

        String firstLineStr = split[0]; // GET / HTTP/1.1
        String[] strings = firstLineStr.split(" ");

        method = strings[0];
        url = strings[1];

        System.out.println("method =====> " + method);
        System.out.println("url =====> " + url);

        String secondLineStr = split[1];
        System.out.println(secondLineStr);
        this.host = secondLineStr.split(" ")[1].replace("\r","");
    }

}
